<template>
  <div id="usermanage">
    <div>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-circle-plus"
        @click="toAdd"
        >新增</el-button
      >
      <div class="widthDiv">
        <el-input
          size="small"
          clearable
          v-model="pageParam.username"
          placeholder="请输入用户名"
        ></el-input>
      </div>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        @click="toSearch"
        >搜索</el-button
      >
      <div class="Router">
        <el-button size="small" type="primary" @click="toRouter"
          >用户数据图表</el-button
        >
      </div>
    </div>
    <div class="tableDiv">
      <el-table
        stripe
        border
        max-height="520px"
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55"></el-table-column> -->
        <el-table-column
          label="序号"
          type="index"
          width="50px"
        ></el-table-column>
        <el-table-column label="用户名" prop="username" width="150px">
          <template slot-scope="scope">
            <i class="el-icon-user-solid"></i>
            <div class="button">
              {{ scope.row.username }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="realname" width="150px">
          <template slot-scope="scope">
            <i class="el-icon-s-custom"></i>
            <div class="button">
              {{ scope.row.realname }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          :filters="[
            { text: '男', value: '男' },
            { text: '女', value: '女' },
          ]"
          :filter-method="filterGender"
          label="性别"
          prop="gender"
          width="80px"
        >
        </el-table-column>
        <el-table-column label="手机号" prop="telephone" width="150px">
          <template slot-scope="scope">
            <!-- <div class="button"> -->
            <i class="el-icon-phone"></i>
            {{ scope.row.telephone }}
            <!-- </div> -->
          </template>
        </el-table-column>
        <el-table-column label="出生日期" prop="birth" width="200px">
          <template slot-scope="scope">
            <i class="el-icon-watch"></i>

            <span>{{
              moment(scope.row.birth).format("YY-MM-DD hh:mm:ss")
            }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="">
          <template slot-scope="scope">
            <!-- <i class="el-icon-edit"></i> -->
            <!-- <div class="button"> -->
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <!-- </div> -->
            <!-- <i class="el-icon-delete-solid"></i> -->

            <!-- <div class="button"> -->
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
            <!-- </div> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageParam.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageParam.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="visible"
      :before-close="beforeClose"
    >
      <el-radio-group v-model="labelPosition" size="small">
        <el-radio-button label="left">左对齐</el-radio-button>
        <el-radio-button label="right">右对齐</el-radio-button>
        <el-radio-button label="top">顶部对齐</el-radio-button>
      </el-radio-group>
      <div style="margin: 20px"></div>
      <el-form
        :label-position="labelPosition"
        label-width="80px"
        status-icon="true"
        :model="form"
        ref="form"
        :rules="rules"
      >
        <el-form-item
          prop="username"
          label="用户名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          label="密码"
          :label-width="formLabelWidth"
        >
          <!-- type="password" -->
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item
          prop="realname"
          label="姓名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.realname"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio value="男" label="男"></el-radio>
            <el-radio value="女" label="女"></el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- <el-form-item label="出生日期" required>
          <el-col :span="11">
            <el-form-item prop="date1">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="form.date1"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item> -->
        <!--  -->
        <el-form-item label="出生日期" required>
          <el-col :span="11">
            <el-form-item prop="birth">
              <!-- editable="false" -->
              <!-- format="yyyy-MM-dd" -->
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="form.birth"
                style="width: 100%"
                value-format="timestamp"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <!--  -->
        <!-- <el-form-item
          prop="birth"
          label="出生日期"
          :label-width="formLabelWidth"
        >
          <el-input v-model.number="form.birth"></el-input>
        </el-form-item> -->
        <el-form-item
          prop="telephone"
          label="手机号"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.telephone"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toCancel">取 消</el-button>
        <el-button type="primary" @click="toSave('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "../utils/request";
var moment = require("moment");
export default {
  data() {
    return {
      drawer: false,
      labelPosition: "left",
      form: {},
      total: 0,
      // 分页查询的参数
      pageParam: {
        page: 1,
        pageSize: 10,
        title: "",
        username: "",
      },
      multipleSelection: [],
      tableData: [],
      // 模态框可见性 dialog  modal
      visible: false,
      // 模态框的数据

      // 模态框表单控件标题的宽度
      formLabelWidth: "80px",
      // 模态框的标题
      title: "录入用户信息",
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        realname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
        // date1: [
        //   {
        //     type: "date",
        //     required: true,
        //     message: "请选择出生日期",
        //     trigger: "change",
        //   },
        // ],
        birth: [
          {
            type: "date",
            required: true,
            message: "请选择出生日期",
            trigger: "change",
          },
        ],
        // birth: [
        //   {
        //     required: true,
        //     message: "请输入出生日期",
        //     trigger: "blur",
        //   },
        // ],
        telephone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
        ],
      },
    };
  },
  computed: {},
  methods: {
    moment,
    toRouter() {
      this.$router.replace("/userechart");
    },
    filterGender(value, row) {
      return row.gender === value;
    },
    toSearch() {
      this.pageParam.page = 1;
      this.pageQuery();
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageParam.pageSize = val;
      this.pageQuery();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageParam.page = val;
      this.pageQuery();
    },
    // 多选
    handleSelectionChange(val) {
      // val是用户选中的数据对象数组
      // val = [ { "id": 3, "name": "最新进展", "description": "疫情的动态", "no": 1, "parentId": null }, { "id": 4, "name": "公告", "description": null, "no": 2, "parentId": null } ] ;
      // this.multipleSelection = [3,4]
      this.multipleSelection = val.map((item) => item.id);
    },
    // 批量删除
    // toBatchDelete() {
    //   console.log("批量删除");
    // },
    // 关闭模态框
    toCancel() {
      this.$refs["form"].resetFields();
      this.visible = false;
    },
    // 模态框关闭之前
    beforeClose(done) {
      this.$refs["form"].resetFields();
      done();
    },
    // 点击新增按钮
    toAdd() {
      this.form = {};
      this.visible = true;
      this.title = "添加用户信息";
    },
    // 点击确定按钮
    toSave(formName) {
      // console.log(this.form);
      // this.form.birth = moment(this.form.birth).format("YY-MM-DD");
      // this.form = { ...this.form, birth: this.form.date1 };
      this.$refs[formName].validate((valid) => {
        // 校验通过
        if (valid) {
          // console.log(this.form, "=====");
          axios.post("/baseUser/saveOrUpdate", this.form).then((res) => {
            if (res.status == 200) {
              //保存成功，关闭模态框，刷新数据
              this.$notify({
                title: "成功",
                message: "保存成功",
                type: "success",
              });
              this.toCancel();
              this.pageQuery();
            } else {
              this.$notify.error({
                title: "失败",
                message: "用户名或手机号已存在，请修改",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 编辑
    handleEdit(row) {
      this.title = "编辑用户信息";
      this.form = { ...row };
      this.visible = true;
    },
    // 删除
    handleDelete(id) {
      // id 就是要删除的用户的对象的id
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // await等待后面异步函数运行完毕，拿到响应的数据，然后返回
          let res;
          if (id) {
            res = await axios.get("/baseUser/deleteById", { params: { id } });
          }
          this.$notify({
            title: res.status == 200 ? "成功" : "失败",
            message: res.statusText,
            type: res.status == 200 ? "success" : "error",
          });
          this.pageQuery();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页查找
    async pageQuery() {
      let res = await axios.get("/baseUser/pageQuery", {
        params: this.pageParam,
      });
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
  },
  created() {
    this.pageQuery();
  },
};
</script>
<style scoped>
/* .tableDiv{
  
} */
/* #usermanagename {
  height: 100%;
  
} */
.tableDiv {
  text-align: center;
  min-height: 520px;
}
.widthDiv {
  display: inline-block;
  margin-left: 20px;
  margin-right: 20px;
}
.button {
  margin-left: 10px;
  display: inline-block;
}

.Router {
  float: right;
  display: inline-block;
  margin-right: 50px;
}
</style>


